package com.example.demo.controller;

import com.example.demo.domain.result.EmptyClassRoomResult;
import com.example.demo.result.ApiResult;
import com.example.demo.result.ResultCode;
import com.example.demo.service.EmptyClassRoomService;
import com.google.gson.Gson;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author lizijian
 */
@RestController
@RequestMapping("/School/classroom")
@Slf4j
@Validated
public class ClassRoomController {

    private static final Gson JSON=new Gson();

    @Autowired
    private EmptyClassRoomService emptyClassRoomService;

    @ApiOperation(value = "查询空教室")
    @GetMapping("/empty")
    public ApiResult<List<EmptyClassRoomResult>> getEmptyClassRoom(@NotBlank @RequestParam("buildingId") String buildingIdString,
                                                                   @NotBlank @RequestParam("week") String weekString,
                                                                   @NotBlank @RequestParam("day") String dayString,
                                                                   @NotBlank @RequestParam("lesson") String lessonString ){
        Integer[] building = JSON.fromJson(buildingIdString, Integer[].class);
        Integer[] week = JSON.fromJson(weekString, Integer[].class);
        Integer[] day = JSON.fromJson(dayString, Integer[].class);
        Integer[] lesson = JSON.fromJson(lessonString, Integer[].class);
        if(building.length+week.length+day.length+lesson.length>8){
            return ApiResult.fail(ResultCode.INVALID_PARAM, "too many parameters","参数过多",null);
        }
        return ApiResult.success(emptyClassRoomService.getEmptyClassRoom(building,week,day,lesson));
    }

}
